# import os
#
# from django.utils.deprecation import MiddlewareMixin
# import time
#
# # 定义一个类继承MiddlewareMixin 并实现下面的方法，在这两个方法 中定义或者拦截对应的请求# 可以在中间件中添加用户认证和登录设置等信息
# class CustomMiddle(MiddlewareMixin):
#     def process_request(self, request):
#         # print('request', request.body)
#
#         current_path = os.path.dirname(__file__)
#         with open(current_path +'\\haha.txt', encoding='utf-8') as f:
#             word = request.body
#             word1 = eval(word.decode('utf-8'))['content']
#             print(eval(word.decode('utf-8'))['content'])
#             for keyword in f:
#                 if keyword == word1:
#                     print('ssssssss')
#         print('replace方法')
#
#
#
# import jieba
# from django.conf import settings
# import time
#
# class DFAFilter():
#     ''' Filter Messages from keywords
#
#     Use DFA to keep algorithm perform constantly
#
#     f = DFAFilter()
#     f.add("sexy")
#     f.filter("hello sexy baby")
#     hello **** baby
#     '''
#
#     def __init__(self):
#         self.stopwords = ['!']
#         self.keyword_chains = {}
#         self.delimit = '\x00'
#         with open(settings.FILTER_PATH, encoding='utf-8') as f:
#             for keyword in f:
#                 self.add(keyword.strip())
#     def add(self, keyword):
#
#         if not isinstance(keyword, str):
#             keyword = keyword.decode('utf-8')
#         keyword = keyword.lower()
#         chars = keyword.strip()
#         if not chars:
#             return
#         level = self.keyword_chains
#         for i in range(len(chars)):
#             if chars[i] in level:
#                 level = level[chars[i]]
#
#             else:
#                 if not isinstance(level, dict):
#                     break
#                 for j in range(i, len(chars)):
#                     level[chars[j]] = {}
#                     last_level, last_char = level, chars[j]
#                     level = level[chars[j]]
#
#                 last_level[last_char] = {self.delimit: 0}
#                 break
#         if i == len(chars) - 1:
#             level[self.delimit] = 0
#     def filter(self, message, repl="*"):
#         stop_list1 = []
#         stop_list2 = []
#         stop_index = -1
#         if not isinstance(message, str):
#             message = message.decode('utf-8')
#         message = message.lower()
#
#         wordlist = jieba.lcut(message)
#         stop = set(self.stopwords)
#         for i in wordlist:
#             if i == ' ' or '':
#                 stop_index = message.index(i, stop_index + 1, len(message))
#                 stop_list1.append(stop_index)
#                 stop_list2.append('')
#                 wordlist.remove(i)
#             if i in stop:
#                 stop_index = message.index(i, stop_index + 1, len(message))
#                 stop_list1.append(stop_index)
#                 stop_list2.append(i)
#                 wordlist.remove(i)
#
#         message = ''.join(wordlist)
#         ret = []
#         start = 0
#         danger = 0
#         while start < len(message):
#             level = self.keyword_chains
#             step_ins = 0
#             for char in message[start:]:
#
#                 if char in level:
#                     step_ins += 1
#
#                     if self.delimit not in level[char]:
#                         level = level[char]
#
#                     else:
#                         danger += 1
#                         ret.append(repl * step_ins)
#                         start += step_ins - 1
#                         break
#
#                 else:
#                     ret.append(message[start])
#                     print(ret)
#                     break
#             else:
#                 ret.append(message[start])
#
#             start += 1
#         result = list(''.join(ret))
#         for i, j in zip(stop_list1, stop_list2):
#             result.insert(i, j)
#
#         return ''.join(result), danger